package main

import "fmt"

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */

type ListNode struct {
	Val  int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	//rl1, _ := reverNode(l1)
	//rl2, _ := reverNode(l2)

	var newNode *ListNode
	var nowNode = newNode

	rl1Now := l1
	rl2Now := l2
	tmp := 0
	for true {
		num1 := 0
		if rl1Now != nil {
			num1 = rl1Now.Val
			rl1Now = rl1Now.Next
		}
		num2 := 0
		if rl2Now != nil {
			num2 = rl2Now.Val
			rl2Now = rl2Now.Next
		}
		tmp = tmp + num1 + num2
		now := tmp
		if tmp >= 10 {
			now = tmp % 10
			tmp = tmp / 10
		} else {
			tmp = 0
		}
		fmt.Println(now)
		if newNode == nil {
			newNode = &ListNode{Val: now}
			nowNode = newNode
		} else {
			nowNode.Next = &ListNode{Val: now}
			nowNode = nowNode.Next
		}

		if tmp == 0 && rl2Now == nil && rl1Now == nil {
			break
		}
	}

	return newNode
}

func reverNode(node *ListNode) (*ListNode, int) {
	var newNode *ListNode
	length := 0
	for ; node != nil; node = node.Next {
		newNode = &ListNode{
			Val:  node.Val,
			Next: newNode,
		}
		length++
	}
	return newNode, length
}

//[2,4,3]
//[5,6,4]
func main() {
	l1 := &ListNode{
		Val: 1,
		Next: &ListNode{
			Val: 8,
		},
	}
	l2 := &ListNode{
		Val: 0,
	}

	addTwoNumbers(l1, l2)

}
